package com.hd.service.doc.article.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.hd.generator.StringUtil;
import com.hd.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hd.dao.BaseDao;
import com.hd.entity.doc.article.Article;
import com.hd.entity.doc.article.ArticleType;
import com.hd.service.doc.article.ArticleTypeService;
import com.hd.util.result.PageInfo;
import com.hd.util.result.Tree;
/**
* 文章类型
*/
@Service("articleTypeService")
public class ArticleTypeServiceImpl implements ArticleTypeService {
    @Autowired
    private BaseDao<ArticleType,String>articleTypeDao;
    @Autowired
    private BaseDao<Article,String>articleDao;
    @Override
    public void save(ArticleType articleType) {
    	articleType.setId(UUID.randomUUID().toString());
       articleTypeDao.save(articleType);
    }

    @Override
    public void delete(String id){
      articleTypeDao.delete(ArticleType.class,id);
      String hql="update Article set type='',typeName='' where type=?";
      articleDao.execute(hql, id);
    }
       

    @Override
    public void update(ArticleType articleType) {
        ArticleType vo=articleTypeDao.get(ArticleType.class,articleType.getId());
        articleTypeDao.update(articleType);
    }
    @Override
    public ArticleType get(String id){
    	return articleTypeDao.get(ArticleType.class, id);
    }
        

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        String hql="from ArticleType where 1=1 ";
        Map<String,Object>condition=pageInfo.getCondition();
        List<Object> params=new ArrayList<Object>();
        if(!pageInfo.getSort().equals("")) {
            hql+=" order by "+pageInfo.getSort()+" "+pageInfo.getOrder();
        }
        List<ArticleType> articleTypes=articleTypeDao.findPage(hql, pageInfo.getNowpage(), pageInfo.getPagesize(),params);
        pageInfo.setRows(articleTypes);
        int total=articleTypeDao.findByList(hql,params).size();
        pageInfo.setTotal(total);
    }
    @Override
	public List<Tree> selectTree(ArticleType articleType) {
		List<ArticleType> articleTypeList = selectTreeGrid(articleType);
		List<Tree> trees = new ArrayList<Tree>();
		if (articleTypeList != null) {
			for (ArticleType vo : articleTypeList) {
				Tree tree = new Tree();
				tree.setId(vo.getId().toString());
				tree.setText(vo.getName());
				tree.setIconCls(vo.getIconCls());
				tree.setPid(StringUtils.isNull(vo.getPid())?"0":vo.getPid().toString());
				trees.add(tree);
			}
		}
		return trees;
	}

	@Override
	public List<ArticleType> selectTreeGrid(ArticleType articleType) {
        List<Object>params=new ArrayList<Object>();
		String hql = "from ArticleType where 1=1 ";
        if(StringUtils.isNotNull(articleType.getName())){
            hql+=" and name like ?";
            params.add("%"+articleType.getName()+"%");
        }
		hql+=" order by seq asc";
		return articleTypeDao.findByList(hql,params);
	}



}
